// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © prowelltraders

//@version=5
indicator("RBLR - GSK Vizag AP India", shorttitle="RBLR", overlay=true, max_lines_count=500, max_labels_count=200)

// === INPUTS ===
showBreakouts = input.bool(true, "Show Signals")

lineStyleOpt = input.string("Dotted", "Line Style", options=["Dotted", "Dashed", "Solid", "Smoothed"])

// === FIXED TIME SETTINGS ===
startHour     = 9
startMinute   = 15
rangeMinutes  = 15
endHour       = 15
endMinute     = 30

// === STYLE FUNCTION ===
getLineStyle(styleStr) =>
    styleStr == "Dotted"  ? line.style_dotted  :     styleStr == "Dashed"  ? line.style_dashed  :     styleStr == "Solid"   ? line.style_solid   :     styleStr == "Smoothed"? line.style_solid   : line.style_dotted

styleSelected = getLineStyle(lineStyleOpt)

// === TIME SESSION HANDLING ===
sessionStart = timestamp("GMT+5:30", year, month, dayofmonth, startHour, startMinute)
sessionEnd   = timestamp("GMT+5:30", year, month, dayofmonth, endHour, endMinute)
rangeEnd     = sessionStart + rangeMinutes * 60 * 1000
inOpening    = time >= sessionStart and time < rangeEnd
newDay       = ta.change(time("D"))

// === OR VARIABLES ===
var float openingHigh = na
var float openingLow = na
var bool rangeLocked = false

// === LINES ===
var line[] dailyHighLines = array.new_line()
var line[] dailyLowLines = array.new_line()
var label[] dailyHighLabels = array.new_label()
var label[] dailyLowLabels = array.new_label()

// === BREAKOUT/REVERSAL STATE ===
var int lastState = 0 // 0 = neutral/in-range, 1 = above ORH, -1 = below ORL

// === RESET DAILY ===
if newDay
    openingHigh := na
    openingLow := na
    rangeLocked := false
    lastState := 0

// === CAPTURE OPENING RANGE ===
if inOpening
    openingHigh := na(openingHigh) ? high : math.max(openingHigh, high)
    openingLow  := na(openingLow)  ? low  : math.min(openingLow, low)

// === DRAW LEVELS ===
if not inOpening and not na(openingHigh) and not rangeLocked
    rangeLocked := true
    lineColorHigh = lineStyleOpt == "Smoothed" ? color.new(color.green, 40) : color.green
    lineColorLow  = lineStyleOpt == "Smoothed" ? color.new(color.red, 40) : color.red
    lineHigh = line.new(bar_index, openingHigh, bar_index + 1, openingHigh,  color=lineColorHigh, style=styleSelected, width=2)
    lineLow = line.new(bar_index, openingLow, bar_index + 1, openingLow,  color=lineColorLow, style=styleSelected, width=2)
    array.push(dailyHighLines, lineHigh)
    array.push(dailyLowLines, lineLow)
    
    // Add labels at the end (initially at start, will be updated)
    labelHigh = label.new(bar_index, openingHigh, "RAM \n BAAN", color=color.rgb(243, 99, 4), textcolor=color.white, style=label.style_label_left, size=size.normal)
    labelLow = label.new(bar_index, openingLow, "LAKSHMAN \n REKHA", color=color.rgb(243, 99, 4), textcolor=color.white, style=label.style_label_left, size=size.normal)
    array.push(dailyHighLabels, labelHigh)
    array.push(dailyLowLabels, labelLow)

// === EXTEND THROUGH SESSION ===
if rangeLocked and time <= sessionEnd
    if array.size(dailyHighLines) > 0 and array.size(dailyLowLines) > 0
        line.set_x2(array.get(dailyHighLines, array.size(dailyHighLines)-1), bar_index)
        line.set_x2(array.get(dailyLowLines, array.size(dailyLowLines)-1), bar_index)
        label.set_x(array.get(dailyHighLabels, array.size(dailyHighLabels)-1), bar_index)
        label.set_x(array.get(dailyLowLabels, array.size(dailyLowLabels)-1), bar_index)

// === LOGIC: MULTIPLE BREAKS & REVERSALS ===
breakUp   = rangeLocked and ta.crossover(close, openingHigh)
breakDown = rangeLocked and ta.crossunder(close, openingLow)

reversalFromUp = lastState == 1 and close < openingHigh
reversalFromDown = lastState == -1 and close > openingLow

// Update state: 1 for above ORH, -1 for below ORL, 0 for in-range
if breakUp
    lastState := 1
if breakDown
    lastState := -1
if reversalFromUp or reversalFromDown
    lastState := 0

// === PLOTS ===
plotshape(showBreakouts and breakUp, title="Break Up", location=location.belowbar,
     style=shape.triangleup, size=size.small, color=color.green, text="Break ↑")

plotshape(showBreakouts and breakDown, title="Break Down", location=location.abovebar,
     style=shape.triangledown, size=size.small, color=color.red, text="Break ↓")

plotshape(showBreakouts and reversalFromUp, title="Reversal Down", location=location.abovebar,
     style=shape.labeldown, size=size.small, color=color.yellow, text="Rev ↓")

plotshape(showBreakouts and reversalFromDown, title="Reversal Up", location=location.belowbar,
     style=shape.labelup, size=size.small, color=color.yellow, text="Rev ↑")